/* * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.stephentuso.welcome.util;

import ohos.agp.components.element.VectorElement;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ResUtil {
    private static final String TAG = ResUtil.class.getSimpleName();

    private ResUtil() {
    }

    /**
     * get the color
     *
     * @param context the context
     * @param id      the id
     * @return the color
     */
    public static int getColor(Context context, int id) {
        int result = 0;
        if (context == null) {
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            return result;
        }
        try {
            result = manager.getElement(id).getColor();
        } catch (IOException | NotExistException | WrongTypeException e) {
            LogUtil.error(TAG, e.getMessage());
        }
        return result;
    }

    /**
     * get boolean
     *
     * @param context the context
     * @param id      the boolean id
     * @return boolean of the given id
     */
    public static boolean getBoolean(Context context, int id) {
        boolean result = false;
        if (context == null) {
            return false;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            return false;
        }
        try {
            result = manager.getElement(id).getBoolean();
        } catch (IOException | NotExistException | WrongTypeException e) {
            LogUtil.error(TAG, e.getMessage());
        }
        return result;
    }

    /**
     * get the vector drawable
     *
     * @param context the context
     * @param id      the drawable id
     * @return the vector drawable
     */
    public static VectorElement getVectorDrawable(Context context, int id) {
        if (context == null) {
            return null;
        }
        return new VectorElement(context, id);
    }

    /**
     * get rawfile file image
     * @param context context
     * @param resPath file name
     * @return file
     */
    public static File resPathToFile(Context context, String resPath) {
        String[] fileNames = resPath.split(File.separator);
        File tempFile = new File(context.getCacheDir() + File.separator + fileNames[fileNames.length - 1]);
        try {
            ResourceManager resourceManager = context.getResourceManager();
            Resource resource = resourceManager.getRawFileEntry("resources/rawfile/"+resPath).openRawFile();
            byte[] buffer = new byte[resource.available()];
            resource.read(buffer);
            FileOutputStream fos = new FileOutputStream(tempFile);
            fos.write(buffer);
            fos.close();
        }catch (Exception e){
            LogUtil.error(TAG, e.getMessage());
            return tempFile;
        }
        return tempFile;
    }
}

